// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Disposable.cs" company="">
//   
// </copyright>
// <summary>
//   The disposable.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Core
{
    using System;
    using System.Threading;

    /// <summary>
    ///   The disposable.
    /// </summary>
    public abstract class Disposable : IDisposable
    {
        #region Fields

        /// <summary>
        ///   The _is disposed.
        /// </summary>
        private volatile int _isDisposed;

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets a value indicating whether is disposed.
        /// </summary>
        public bool IsDisposed
        {
            get
            {
                return this._isDisposed == 1;
            }
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The dispose.
        /// </summary>
        public void Dispose()
        {
#pragma warning disable 420
            if (Interlocked.CompareExchange(ref this._isDisposed, 1, 0) == 0)
#pragma warning restore 420
            {
                this.DoDispose();
            }
        }

        #endregion

        #region Methods

        /// <summary>
        ///   The do dispose.
        /// </summary>
        protected abstract void DoDispose();

        #endregion
    }
}